package com.example.myapplication01.jetpack.navigation.viewmodel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.myapplication01.R;
import com.example.myapplication01.databinding.FragmentAddBinding;

import java.util.Objects;

public class AddFragment extends Fragment {

    public static AddFragment newInstance() {
        return new AddFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        MyViewModel mViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
        FragmentAddBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_add, container, false);
        binding.setData(mViewModel);
        binding.setLifecycleOwner(requireActivity());
        binding.addBtn.setOnClickListener(v -> {
            mViewModel.add();
            Integer val = mViewModel.getNumber().getValue();
            if (Objects.equals(val, 5)) {
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_addFragment_to_subFragment);
            }
        });

        // return inflater.inflate(R.layout.fragment_add, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}